package com.czy.owner.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.czy.owner.R;


/**
 * Created by King on 2017-05-15
 */

public class KingPoint extends View {
    private float spaceOffset = 0f;
    private int pointType = 0;

    private float reachedCircleWidth = 1;
    private int reachedColor = Color.parseColor("#FF09C0C7");
    private int unReachedColor = Color.parseColor("#3309C0C7");

    private float centerX, centerY, radius;

    public KingPoint(Context context) {
        super(context);
    }

    public KingPoint(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public KingPoint(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        reachedCircleWidth = dip2px(context, 1);
        spaceOffset = 0;
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.KingPoint);
        pointType = ta.getInt(R.styleable.KingPoint_point_type, 0);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


        centerX = getWidth() / 2;
        centerY = getHeight() / 2;

        if (getWidth() > getHeight()) {
            radius = getHeight() * 2 / 12;
        } else {
            radius = getWidth() * 2 / 12;
        }


        drawSolidCircle(canvas);
        if (pointType == 1) {
            drawHollowCircle(canvas);
        }
    }

    private void drawSolidCircle(Canvas canvas) {
        Paint solidPaint = new Paint();
        if (pointType == 2) {
            solidPaint.setColor(unReachedColor);
        } else {
            solidPaint.setColor(reachedColor);
        }
        solidPaint.setAntiAlias(true);
        solidPaint.setStyle(Paint.Style.FILL);
        //画实心圆
        canvas.drawCircle(centerX, centerY, radius, solidPaint);
    }

    private void drawHollowCircle(Canvas canvas) {
        Paint hollowPaint = new Paint();
        hollowPaint.setColor(unReachedColor);
        hollowPaint.setAntiAlias(true);
        hollowPaint.setStyle(Paint.Style.STROKE);
        hollowPaint.setStrokeWidth(reachedCircleWidth);

        for (int i = 1; i < 4; i++) {
            canvas.drawCircle(centerX, centerY, radius + radius * i / 2, hollowPaint);
        }
    }

    /**
     * @param pointType 仅支持 0 , 1 ,2
     */
    public void setPointType(int pointType) {
        this.pointType = pointType;
        invalidate();
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
